//
//  RHimageItemView.m
//  RuiHeApp
//
//  Created by bill on 2025/3/30.
//

#import "RHAddImageCell.h"
#import "RHAddImageModel.h"
@interface RHAddImageCell()
@property (nonatomic, strong) QMUIButton *addBtn;

@property (nonatomic, strong) QMUIButton *deleteBtn;

@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation RHAddImageCell

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return  self;
}

-(void)setImgModel:(RHAddImageModel *)imgModel
{
    _imgModel = imgModel;
    if(imgModel.image){
        self.imageView.image = imgModel.image;
    }else{
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:imgModel.imageUrl]placeholderImage:[UIImage imageNamed:@""]];
    }
    self.deleteBtn.hidden = !imgModel.isEditing;
    self.addBtn.hidden = !((imgModel.imageUrl == nil) && (imgModel.image == nil));
}


-(void)showNormalStatus
{
    self.imageView.hidden = NO;
    self.deleteBtn.hidden = YES;
    self.addBtn.hidden = YES;
}
-(void)showDeleteStatus
{
    self.imageView.hidden = NO;
    self.deleteBtn.hidden = NO;
    self.addBtn.hidden = YES;
}
-(void)showAddStatus
{
    self.imageView.hidden = YES;
    self.deleteBtn.hidden = YES;
    self.addBtn.hidden = NO;
}


-(void)setupUI
{
    kWeakSelf;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    _imageView = kImageView;
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    _imageView.layer.cornerRadius = 8;
    _imageView.layer.masksToBounds = YES;
    _imageView.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
    [bgView addSubview:_imageView];
    
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    _addBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [_addBtn.layer setCornerRadius:8];
    [_addBtn setBackgroundColor:kCOLOR_HEX(@"#F3F7FF")];
    [_addBtn setImagePosition:QMUIButtonImagePositionTop];
    [_addBtn setImage:[UIImage imageNamed:@"kwt_plant_camera"] forState:UIControlStateNormal];
    [_addBtn setTitle:@"添加图片".rh_localized forState:UIControlStateNormal];
    [_addBtn.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [_addBtn setSpacingBetweenImageAndTitle:5];
    [_addBtn setTitleColor:kCOLOR_HEX(@"AAAAAA") forState:UIControlStateNormal];
    [bgView addSubview:_addBtn];
    [_addBtn addTarget:self action:@selector(addPictureAction) forControlEvents:UIControlEventTouchUpInside];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
 
    _deleteBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [_deleteBtn setImage:[UIImage imageNamed:@"kwt_pic_delete"] forState:UIControlStateNormal];
    [_deleteBtn setBackgroundColor:UIColor.clearColor];
    [bgView addSubview:_deleteBtn];
    [_deleteBtn addTarget:self action:@selector(deletePictureAction) forControlEvents:UIControlEventTouchUpInside];
    [_deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.right.mas_offset(0);
        make.top.mas_offset(0);
    }];
        
}

-(void)addPictureAction
{
    if (self.addPictureBlcok) {
        self.addPictureBlcok(self.imgModel);
    }
}
-(void)deletePictureAction
{
    if (self.deletePictureBlcok) {
        self.deletePictureBlcok(self.imgModel);
    }
}
@end
